package com.cuitrip.business.tripservice.ui;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.cuitrip.business.tripservice.model.SelectMenu;
import com.cuitrip.component.wheel.OnWheelScrollListener;
import com.cuitrip.component.wheel.WheelView;
import com.cuitrip.component.wheel.adapters.WheelViewAdapter;
import com.cuitrip.service.R;
import com.lab.component.common.BaseDialogFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by ak
 */
public class SelectMenuFragment extends BaseDialogFragment implements View.OnClickListener {

    @Bind(R.id.wheel_view)
    WheelView genderWheelView;

    @Bind(R.id.btnCancel)
    Button btnCancel;

    @Bind(R.id.btnConfirm)
    Button btnConfirm;

    @Bind(R.id.title)
    TextView titleView;

    private String title;

    private SelectWheelAdapter genderWheelAdapter;
    private List<SelectMenu> selectMenuList = new ArrayList<>();
    private int position;

    public void setSelectMenuList(List<SelectMenu> selectMenuList) {
        this.selectMenuList = selectMenuList;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.contextMenu);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.select_menu_list, container, false);
        ButterKnife.bind(this, view);
        btnCancel.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
        if (!TextUtils.isEmpty(title)){
            titleView.setText(title);
        }
        genderWheelView.setVisibleItems(5);
        genderWheelAdapter = new SelectWheelAdapter(selectMenuList);
        genderWheelView.setViewAdapter(genderWheelAdapter);
        genderWheelView.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {
            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                position = genderWheelView.getCurrentItem();

            }
        });

        return view;
    }

    @OnClick(R.id.context_menu_blank)
    void onBlankClick(){
        hideDialogFragment(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btnConfirm:
                SelectMenu selectMenu = selectMenuList.get(position);
                mDataSendBackListenr.onSelect(selectMenu);
                dismiss();
                break;
            case R.id.btnCancel:
                dismiss();
                break;
        }

    }

    private class SelectWheelAdapter implements WheelViewAdapter {


        private List<SelectMenu> selectMenus = new ArrayList<>();

        public SelectWheelAdapter(List<SelectMenu> selectMenuList) {
            this.selectMenus = selectMenuList;
        }

        @Override
        public int getItemsCount() {
            return selectMenus == null ? 0 : selectMenus.size();
        }

        @Override
        public View getItem(int index, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gender_show, parent, false);
            }
            ((TextView) convertView).setText(selectMenus.get(index).value);
            ((TextView) convertView).setTextSize(17.f);
            return convertView;
        }

        @Override
        public View getEmptyItem(View convertView, ViewGroup parent) {
            return null;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {

        }
    }

    public interface OnDataSelectBackListener {
        public void onSelect(SelectMenu selectMenu);
    }

    private OnDataSelectBackListener mDataSendBackListenr;

    public void setResultListener(OnDataSelectBackListener listener) {
        mDataSendBackListenr = listener;
    }


}
